import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: () => import('../views/login.vue'),
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/home.vue'),
      redirect: '/preview',
      meta: {
        breadcrumb: [
          { to: '/home', text: '首页' },
          // { to: '/carrecord', text: '车辆管理' },
          { text: '首页预览' },
        ]
      },
      children: [
        {
          path: '/preview',
          name: 'preview',
          component: () => import('../views/preview.vue'),
        },
        {
          path: '/contractlist',
          name: 'contractlist',
          component: () => import('../views/contract/contract_list.vue'),
        },
        {
          path: '/contracttype',
          name: 'contracttype',
          component: () => import('../views/contract/contract_type.vue'),
        },
        {
          path: '/epidemicrecord',
          name: 'epidemicrecord',
          component: () => import('../views/epidemic/epidemic_record.vue'),
          meta: {
            breadcrumb: [
              { to: '/epidemicrecord', text: '疫情防控' },
              { text: '防控信息' },
            ]
          },
          // meta: {
          //   breadcrumb: [
          //     { to: '/epidemicrecord', text: '疫情防控' },
          //     // { to: '/carrecord', text: '车辆管理' },
          //     { text: '防控信息' },
          //   ]
          // },
        },
        {
          path: '/addepidemic',
          name: 'addepidemic',
          component: () => import('../views/epidemic/addepidemic.vue'),
          meta: {
            breadcrumb: [
              { to: '/addepidemic', text: '疫情防控' },
              { text: '添加防控' },
            ]
          },
        },
        {
          path: '/carrecord',
          name: 'carrecord',
          component: () => import('../views/car/car_record.vue'),
          meta: {
            breadcrumb: [
              { to: '/carrecord', text: '车辆管理' },
              { text: '全部车辆' },
            ]
          },
        },
        {
          path: '/addcar',
          name: 'addcar',
          component: () => import('../views/car/addcar.vue'),
          meta: {
            breadcrumb: [
              { to: '/addcar', text: '车辆管理' },
              { text: '录入车辆' },
            ]
          },
        },
        {
          path: '/user',
          name: 'user',
          component: () => import('../views/permissions/user.vue'),
          meta: {
            breadcrumb: [
              { to: '/user', text: '权限管理' },
              { text: '用户管理' },
            ]
          },
        },
        {
          path: '/ownerslist',
          name: 'ownerslist',
          component: () => import('../views/owners/owners_list.vue'),
          meta: {
            breadcrumb: [
              { to: '/ownerslist', text: '业主档案' },
              { text: '业主列表' },
            ]
          },
        },
        {
          path: '/addowners',
          name: 'addowners',
          component: () => import('../views/owners/addowners.vue'),
          meta: {
            breadcrumb: [
              { to: '/addowners', text: '业主档案' },
              { text: '添加业主' },
            ]
          },
        },
        {
          path: '/ownersxiang',
          name: 'ownersxiang',
          component: () => import('../views/owners/owners_xiang.vue'),
          meta: {
            breadcrumb: [
              { to: '/ownersxiang', text: '业主档案' },
              { text: '业主详情' },
            ]
          },
        },
        {
          path: '/houselist',
          name: 'houselist',
          component: () => import('../views/house/house_list.vue'),
          meta: {
            breadcrumb: [
              { to: '/houselist', text: '房产管理' },
              { text: '房产列表' },
            ]
          },
        },
        {
          path: '/addhouse',
          name: 'addhouse',
          component: () => import('../views/house/addhouse.vue'),
          meta: {
            breadcrumb: [
              { to: '/addhouse', text: '房产管理' },
              { text: '添加房产' },
            ]
          },
        },
        {
          path: '/housexiang',
          name: 'housexiang',
          component: () => import('../views/house/house_xiang.vue'),
          meta: {
            breadcrumb: [
              { to: '/housexiang', text: '房产管理' },
              { text: '房产详情' },
            ]
          },
        },
        {
          path: '/addchewei',
          name: 'addchewei',
          component: () => import('../views/chewei/addchewei.vue'),
          meta: {
            breadcrumb: [
              { to: '/addchewei', text: '车位管理' },
              { text: '添加车位' },
            ]
          },
        },
        {
          path: '/cheweilist',
          name: 'cheweilist',
          component: () => import('../views/chewei/chewei_list.vue'),
          meta: {
            breadcrumb: [
              { to: '/cheweilist', text: '车位管理' },
              { text: '车位列表' },
            ]
          },
        },
        {
          path: '/addweixiu',
          name: 'addweixiu',
          component: () => import('../views/weixiu/addweixiu.vue'),
          meta: {
            breadcrumb: [
              { to: '/addweixiu', text: '维修维护' },
              { text: '添加维修' },
            ]
          },
        },
        {
          path: '/weixiulist',
          name: 'weixiulist',
          component: () => import('../views/weixiu/weixiu_list.vue'),
          meta: {
            breadcrumb: [
              { to: '/weixiulist', text: '维修维护' },
              { text: '维修列表' },
            ]
          },
        },
        {
          path: '/addzhuangxiu',
          name: 'addzhuangxiu',
          component: () => import('../views/zhuangxiu/addzhuangxiu.vue'),
          meta: {
            breadcrumb: [
              { to: '/addzhuangxiu', text: '装修管理' },
              { text: '添加装修' },
            ]
          },
        },
        {
          path: '/zhuangxiulist',
          name: 'zhuangxiulist',
          component: () => import('../views/zhuangxiu/zhuangxiu_list.vue'),
          meta: {
            breadcrumb: [
              { to: '/zhuangxiulist', text: '装修管理' },
              { text: '装修列表' },
            ]
          },
        },
        {
          path: '/addfangke',
          name: 'addfangke',
          component: () => import('../views/fangke/addfangke.vue'),
          meta: {
            breadcrumb: [
              { to: '/addfangke', text: '访客管理' },
              { text: '添加访客' },
            ]
          },
        },
        {
          path: '/fangkelist',
          name: 'fangkelist',
          component: () => import('../views/fangke/fangke_list.vue'),
          meta: {
            breadcrumb: [
              { to: '/fangkelist', text: '访客管理' },
              { text: '全部访客' },
            ]
          },
        },
        {
          path: '/addnenghao',
          name: 'addnenghao',
          component: () => import('../views/nenghao/addnenghao.vue'),
          meta: {
            breadcrumb: [
              { to: '/addnenghao', text: '能耗管理' },
              { text: '添加能耗' },
            ]
          },
        },
        {
          path: '/nenghaolist',
          name: 'nenghaolist',
          component: () => import('../views/nenghao/nenghao_list.vue'),
          meta: {
            breadcrumb: [
              { to: '/nenghaolist', text: '能耗管理' },
              { text: '能耗列表' },
            ]
          },
        },
        {
          path: '/addjiankong',
          name: 'addjiankong',
          component: () => import('../views/jiankong/addjiankong.vue'),
          meta: {
            breadcrumb: [
              { to: '/addjiankong', text: '监控管理' },
              { text: '添加监控' },
            ]
          },
        },
        {
          path: '/jiankonglist',
          name: 'jiankonglist',
          component: () => import('../views/jiankong/jiankong_list.vue'),
          meta: {
            breadcrumb: [
              { to: '/jiankonglist', text: '监控管理' },
              { text: '监控列表' },
            ]
          },
        },
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  const screenUser = JSON.parse(localStorage.getItem('ScreenUser'))
  console.log(screenUser)
  if (to.path === '/home', to.path === '/contractlist', to.path === '/contracttype') {
    if (screenUser) {
      next()
    } else {
      next('/') // 如果不存在 ScreenUser，则返回登录页
    }
  } else {
    next() // 其他页面不需要检查缓存
  }
})

export default router
